package wdfile

import (
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
)

//PathExists ...判断路径是否存在
func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

//GetFileSize  ..获取文件大小
func GetFileSize(filename string) (result int64) {
	filepath.Walk(filename, func(path string, f os.FileInfo, err error) error {
		result = f.Size()
		return nil
	})
	return
}

//ReadFileByLoadPathorURL 加载文件,文件是本地或网络
func ReadFileByLoadPathorURL(pu string)(data []byte,err error){
	if pu[:4]=="http"{
		if resp,err:=http.Get(pu);err!=nil{
			return nil,err
		}else {
			return ioutil.ReadAll(resp.Body)
		}
	}else {
		return ioutil.ReadFile(pu)
	}
	return
}


//生成路径
func OpenFileOrCreate(path string)(*os.File,error){
	ok , _ := PathExists(path)
	if !ok{
		dir,_ :=filepath.Split(path)
		err := os.MkdirAll(dir,666)
		if err!=nil{
			return  nil,err
		}
		return os.Create(path)
	}
	return os.Open(path)
}